// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.warehouse.models;

import java.util.ArrayList;
import java.util.List;

import org.dengues.core.warehouse.ENavNodeType;
import org.dengues.core.warehouse.ENodeCategoryName;
import org.dengues.core.warehouse.ENodeStatus;
import org.dengues.core.warehouse.IWarehouseNode;
import org.dengues.core.warehouse.IWarehouseObject;
import org.dengues.model.database.DatabaseDiagram;
import org.dengues.model.warehouse.ProcessType;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.swt.graphics.Image;

/**
 * zx class global comment. Detailled comment <br/>
 */
public class WarehouseNode implements IWarehouseNode {

    private static final long serialVersionUID = 12343242343323L;

    private ENavNodeType nodeType;

    private ENodeCategoryName categoryName;

    private IWarehouseNode parent;

    private String label;

    private List<IWarehouseNode> children = new ArrayList<IWarehouseNode>();

    private boolean isInitialized = false;

    private IWarehouseObject object;

    private ENodeStatus nodeStatus;

    public WarehouseNode(IWarehouseObject object, IWarehouseNode parent, ENavNodeType type) {
        this.parent = parent;
        this.nodeType = type;
        this.object = object;
        nodeStatus = ENodeStatus.NORMAL;
    }

    public IWarehouseNode getParent() {
        return parent;
    }

    public void setParent(WarehouseNode parent) {
        this.parent = parent;
    }

    public ENodeCategoryName getCategoryName() {
        return categoryName;
    }

    public void setCategoryName(ENodeCategoryName categoryName) {
        this.categoryName = categoryName;
    }

    public ENavNodeType getNodeType() {
        return nodeType;
    }

    public void setNodeType(ENavNodeType nodeType) {
        this.nodeType = nodeType;
    }

    public List<IWarehouseNode> getChildren() {
        return this.children;
    }

    public void setChildren(List<IWarehouseNode> children) {
        this.children = children;
    }

    private Image image;

    /**
     * Getter for image.
     * 
     * @return the image
     */
    public Image getImage() {
        return this.image;
    }

    /**
     * Sets the image.
     * 
     * @param image the image to set
     */
    public void setImage(Image image) {
        this.image = image;
    }

    /**
     * Getter for label.
     * 
     * @return the label
     */
    public String getLabel() {
        if (this.label == null) {
            if (object != null) {
                if (object.getData() instanceof DatabaseDiagram) {
                    return ((DatabaseDiagram) object.getData()).getName();
                } else if (object.getData() instanceof ProcessType) {
                    return ((ProcessType) object.getData()).getName();
                }
            }
            if (categoryName != null) {
                return categoryName.getDisplayName();
            }
        }
        return this.label;
    }

    /**
     * Sets the label.
     * 
     * @param label the label to set
     */
    public void setLabel(String label) {
        this.label = label;
    }

    private boolean isBin;

    /**
     * Getter for isBin.
     * 
     * @return the isBin
     */
    public boolean isBin() {
        return this.isBin;
    }

    /**
     * Sets the isBin.
     * 
     * @param isBin the isBin to set
     */
    public void setBin(boolean isBin) {
        this.isBin = isBin;
    }

    /**
     * Getter for isInitialized.
     * 
     * @return the isInitialized
     */
    public boolean isInitialized() {
        return this.isInitialized;
    }

    /**
     * Sets the isInitialized.
     * 
     * @param isInitialized the isInitialized to set
     */
    public void setInitialized(boolean isInitialized) {
        this.isInitialized = isInitialized;
    }

    /**
     * Getter for object.
     * 
     * @return the object
     */
    public IWarehouseObject getObject() {
        return this.object;
    }

    /**
     * Sets the object.
     * 
     * @param object the object to set
     */
    public void setObject(IWarehouseObject object) {
        this.object = object;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object arg0) {
        if (arg0 == this) {
            return true;
        }
        if (arg0 == null) {
            return false;
        }
        if (getClass() != arg0.getClass()) {
            return false;
        }
        if (!(arg0 instanceof WarehouseNode)) {
            return false;
        }
        WarehouseNode node = (WarehouseNode) arg0;
        if (node.getLabel() == null) {
            if (getLabel() != null) {
                return false;
            }
        } else {
            if (!node.getLabel().equals(getLabel())) {
                return false;
            }
        }
        if (!node.getCategoryName().equals(getCategoryName())) {
            return false;
        }
        if (node.isBin() != isBin()) {
            return false;
        }
        if (!node.getNodeType().equals(getNodeType())) {
            return false;
        }
        return true;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append(getNodeType() + "->"); //$NON-NLS-1$
        builder.append(getCategoryName());
        builder.append(": "); //$NON-NLS-1$
        builder.append(getLabel());
        return builder.toString();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.warehouse.IWarehouseNode#getWarehousePath()
     */
    public IPath getWarehousePath() {
        if (this == null) {
            return null;
        }
        if (isBin()) {
            return new Path("bin"); //$NON-NLS-1$
        }
        if (getNodeType().compareTo(ENavNodeType.SYSTEM_FOLDER) == 0) {
            return new Path(""); //$NON-NLS-1$
        }

        if (getNodeType() == ENavNodeType.SIMPLE_FOLDER) {
            return getParent().getWarehousePath().append(getLabel());
        } else {
            return getParent().getWarehousePath();
        }
    }

    /**
     * Getter for nodeStatus.
     * 
     * @return the nodeStatus
     */
    public ENodeStatus getNodeStatus() {
        return this.nodeStatus;
    }

    /**
     * Sets the nodeStatus.
     * 
     * @param nodeStatus the nodeStatus to set
     */
    public void setNodeStatus(ENodeStatus nodeStatus) {
        this.nodeStatus = nodeStatus;
    }

}
